package com.welcome_2.beta;

import java.util.ArrayList;
import java.util.HashMap;

import android.R;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DbTools extends SQLiteOpenHelper{
	private static final String TAG = DbTools.class.getSimpleName();
	public static final String DB_NAME  = "timeline.db";
	public static final int DB_VERSION = 1;
	
	Context context;
	
	public DbTools(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.context = context;
		// TODO Auto-generated constructor stub
	}


	@Override
	public void onCreate(SQLiteDatabase database) {
		String query = "CREATE TABLE contacts ( contactId INTEGER PRIMARY KEY, "+
		"nickname TEXT , phoneNumber TEXT, macAddress TEXT)";
		//sql = context.getString(R.string.sql);
			
		
		//*******This is the table provided*******
		database.execSQL(query);
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String query = "DROP TABLE IF EXISTS contacts";
		
		database.execSQL(query);
	
		onCreate(database);
	}

	public void insertContact(HashMap<String, String> queryValues){
		SQLiteDatabase database = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("nickname", queryValues.get("nickname"));
		values.put("phoneNumber", queryValues.get("phoneNumber"));
		values.put("macAddress", queryValues.get("macAddress"));
		
		database.insert("contacts", null, values);
		
		database.close();
		
	}
	
	public int updateContact(HashMap<String, String> queryValues){
		SQLiteDatabase database = this.getWritableDatabase();
				
				ContentValues values = new ContentValues();
				values.put("nickname", queryValues.get("nickname"));
				values.put("phoneNumber", queryValues.get("phoneNumber"));
				values.put("macAddress", queryValues.get("macAddress"));
				
		return database.update("contacts", values, 
				"contactId" + "= " , new String[] {queryValues.get("contactId")});
				
			}
	
	public void deleteContact(String id){
		SQLiteDatabase database = this.getWritableDatabase();
		
		String deleteQuery = "DELETE FROM contacts WHERE contactId='" + id + "'";
		
		database.execSQL(deleteQuery);	
	
	}
	
}
	
